package parser

import (
	"crawler/engine"
	"crawler/model"
	"fmt"
	"regexp"
	"strconv"
)

var photoList=regexp.MustCompile(`[^data_src="]*data_src="(http://photo14.zastatic.com/images/photo/[^"]*)"`)
var Name=regexp.MustCompile(`(<p class="info_item">[^<]*<span class="nick_name">昵称</span>[^<]*<span>([^<]*)</span>[^<]*</p>)`)
var Age=regexp.MustCompile(`(<p class="info_item">[^<]*<span class="nick_name">年龄</span>[^<]*<span>([^岁]*)岁</span>[^<]*</p>)`)
var Workarea=regexp.MustCompile(`(<p class="info_item">[^<]*<span class="nick_name">工作地</span>[^<]*<span>([^<]*)</span>[^<]*</p>)`)
var Height=regexp.MustCompile(`(<p class="info_item">[^<]*<span class="nick_name">身高</span>[^<]*<span>([^CM]*)CM</span>[^<]*</p>)`)
var Income=regexp.MustCompile(`(<p class="info_item">[^<]*<span class="nick_name">月收入</span>[^<]*<span>([^<]*)</span>[^<]*</p>)`)
var Education=regexp.MustCompile(`(<p class="info_item">[^<]*<span class="nick_name">学历</span>[^<]*<span>([^<]*)</span>[^<]*</p>)`)
var Marriage=regexp.MustCompile(`(<p class="info_item">[^<]*<span class="nick_name">婚姻状况</span>[^<]*<span>([^<]*)</span>[^<]*</p>)`)
var House=regexp.MustCompile(`(<p class="info_item">[^<]*<span class="nick_name">住房情况</span>[^<]*<span>([^<]*)</span>[^<]*</p>)`)
var Car=regexp.MustCompile(`(<p class="info_item">[^<]*<span class="nick_name">买车情况</span>[^<]*<span>([^<]*)</span>[^<]*</p>)`)
var Occupation=regexp.MustCompile(`(<p class="info_item">[^<]*<span class="nick_name">职业</span>[^<]*<span>([^<]*)</span>[^<]*</p>)`)
var Hokou=regexp.MustCompile(`(<p class="info_item">[^<]*<span class="nick_name">籍贯</span>[^<]*<span>([^<]*)</span>[^<]*</p>)`)
var Weight=regexp.MustCompile(`(<p class="info_item">[^<]*<span class="nick_name">体重</span>[^<]*<span>([^KG]*)KG</span>[^<]*</p>)`)




func ParsePorfile(content []byte) engine.ParseResult  {
	result:=engine.ParseResult{}
    match:=photoList.FindAllSubmatch(content,-1)
	profile:=model.Porfile{}
	var checkImageUrl= map[string]string{}
	for _,m:=range match{
		if checkImageUrl[string(m[1])] ==""{
			profile.ImagesList=append(profile.ImagesList,string(m[1]))
		}
	}
	name,err:=GetRegexConten(content,Name)
	if err==nil{
		profile.Name=string(name)
	}
	age,err:=GetRegexConten(content,Age)
	if err==nil{
		intAge,err:=strconv.Atoi(string(age))
		if err ==nil{
			profile.Age=intAge
		}
	}
	height,err:=GetRegexConten(content,Height)
	if err==nil{
		intHeight,err:=strconv.Atoi(string(height))
		if err ==nil{
			profile.Height=intHeight
		}
	}
	weight,err:=GetRegexConten(content,Weight)
	if err==nil{
		intWeight,err:=strconv.Atoi(string(weight))
		if err ==nil{
			profile.Weight=intWeight
		}
	}
	workarea,err:=GetRegexConten(content,Workarea)
	if err==nil{
		profile.Workarea=string(workarea)
	}
	income,err:=GetRegexConten(content,Income)
	if err==nil{
		profile.Income=string(income)
	}
	education,err:=GetRegexConten(content,Education)
	if err==nil{
		profile.Education=string(education)
	}
	marriage,err:=GetRegexConten(content,Marriage)
	if err==nil{
		profile.Marriage=string(marriage)
	}
	house,err:=GetRegexConten(content,House)
	if err==nil{
		profile.House=string(house)
	}
	car,err:=GetRegexConten(content,Car)
	if err==nil{
		profile.Car=string(car)
	}
	occupation,err:=GetRegexConten(content,Occupation)
	if err==nil{
		profile.Occupation=string(occupation)
	}
	hokou,err:=GetRegexConten(content,Hokou)
	if err==nil{
		profile.Hokou=string(hokou)
	}
	result.Items=append(result.Items,profile)

    return result
}

func GetRegexConten(content []byte,regxp *regexp.Regexp)([]byte,error ){

	getString:=regxp.FindSubmatch(content)
	if len(getString)<2{
		return nil,fmt.Errorf("匹配失败")
	}

	return getString[2],nil
}